using System;
using Sce.PlayStation.Core;

namespace Nyanimals
{
    /// <summary>
    /// Collision plane class.
    /// TODO: doesnt appear visually, make it
    /// </summary>
	public class CollisionPlane : CollisionShape
	{
		private Vector3 position;
		private Vector3 normal;
        private float offset;
		
#if SHOWCOLLISION
		CStaticContext planeContext;
        public CStaticContext Context
        {
            get { return planeContext;}
            set { planeContext = value;}
        }
#endif
		
		public override Vector3 Position
		{
			get { return position;}
			set 
			{ 
				position = value;
                CalculateOffset();
#if SHOWCOLLISION
				planeContext.WorldMatrix = BuildMatrix();		
#endif
			}
		}
		public Vector3 Normal
		{
			get { return normal;}
			set 
			{
				normal = value;
                CalculateOffset();
#if SHOWCOLLISION
				planeContext.WorldMatrix = BuildMatrix();	
#endif
			}
		}
        
        public float Offset
        {
            get { return offset;}
        }
        
		public override bool Draw
		{
			get { return bDraw;}
			set 
            {
                bDraw = value;
                
#if SHOWCOLLISION
                planeContext.Active = value;
#endif
            }
		}
				
         public CollisionPlane(Vector3 pos, Vector3 norm)
         {
             position = pos;
             normal = norm;
             CalculateOffset();
             
#if SHOWCOLLISION
             // load in the plane mesh
             int meshIndex = CMeshManager.Instance.LoadMesh("debug/normal.SRM");         
             int contextIndex = CContextManager.Instance.AddStaticContext(meshIndex, BuildMatrix());
            
             planeContext = (CStaticContext)CContextManager.Instance.GetContext(contextIndex);
             
             planeContext.Active = bDraw;           
             planeContext.WireFrame = true;
#endif
         }
         ~CollisionPlane()
         {
            Clear();
         }
        private void CalculateOffset()
        {
            offset =  CMath.Dot_Product(position, normal);
        }
		
        public override void Clear()
        {
            
#if SHOWCOLLISION
            if(planeContext != null)
            {
                CContextManager.Instance.RemoveContext(planeContext);
                planeContext = null;
            }
#endif
        }
        
        
        private Matrix4 BuildMatrix()
        {
            Matrix4 mat = Matrix4.Identity;
            mat.AxisZ = normal.Normalize();
            if(mat.AxisZ == Vector3.UnitY)
            {
                mat.AxisX = Vector3.UnitX;
            }
            else if(mat.AxisZ == -Vector3.UnitY)
            {
                mat.AxisX = -Vector3.UnitX;
            }
            else
            {
                mat.AxisX = Vector3.UnitY.Cross(mat.AxisZ).Normalize();
            }
            mat.AxisY = mat.AxisZ.Cross(mat.AxisX).Normalize();
            mat.AxisW = position; 
            
            return mat;
        }
         
        
		public override CollisionTypes Type 
		{
			get { return CollisionTypes.PLANE;}
		}
		
		public override void Render ()
		{
		}

		public override bool Collides (ref CollisionShape other)
		{
			switch(other.Type)
			{
				case CollisionTypes.PLANE:
				{
				} break;				
			};
			
			return false;
		}
	}
}

